package com.hyk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.domain.Config;
import com.hyk.mapper.ConfigMapper;
import com.hyk.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Config getConfigByCode(String code) {

        //先在redis中查找
        List<Config> configs = (List<Config>) redisTemplate.opsForValue().get("SMS_CONFIG");
        Config configx = null;
        for (Config config : configs) {
            if (config.getCode().equals(code)) {
                //configx = config;
                return config;
            }
        }
        //从数据库中查找

        QueryWrapper<Config> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        configx = getById(wrapper);
        if (configx != null){
            redisTemplate.opsForValue().set("SMS_CONFIG",list());
        }
        return configx;
    }
}
